﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using WebBanThuoc.DTO;

namespace WebBanThuoc.DAO
{
    public class ChiTietDonHang_DAO
    {
        private SqlConnection connection;
        //public bool hasData { set; get; }
        public ChiTietDonHang_DAO(SqlConnection connection)
        {
            this.connection = connection;
        }

        private List<ChiTietDonHang> getListBy(string whereCom, string orderCom)
        {
            List<ChiTietDonHang> data = new List<ChiTietDonHang>();
            string comStr = "Select * from [ChiTietDonHang]";
            if (whereCom.Trim() != "")
                comStr += " where " + whereCom;
            if (orderCom.Trim() != "")
            {
                comStr += " order by " + orderCom;
            }

            try
            {
                IDataProvider dap = new DataProvider(connection);
                dap.ExecuteReader_Command(System.Data.CommandType.Text, comStr);
                //this.hasData = dap.HasData;
                if (dap.HasData)
                {
                    while (dap.DaReader.Read())
                    {
                        ChiTietDonHang obj = new ChiTietDonHang();

                        obj.MaDonHang = dap.DaReader["MaDonHang"].ToString();
                        obj.MaSanPham = dap.DaReader["MaSanPham"].ToString();
                        obj.SoLuong = float.Parse(dap.DaReader["SoLuong"].ToString());
                        obj.Gia = Convert.ToDouble(dap.DaReader["Gia"]);
                        obj.GiamGia = Convert.ToDouble(dap.DaReader["GiamGia"]);
                        obj.IsStatus = Convert.ToInt32(dap.DaReader["IsStatus"]);

                        data.Add(obj);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return data;
        }

        public List<ChiTietDonHang> getListByKey()
        {
            return getListBy("", " [MaDonHang] asc, [MaSanPham] asc");
        }
        public List<ChiTietDonHang> getListByKey(string MaDonHang)
        {
            return getListBy(" [MaDonHang]='" + MaDonHang + "' ", " [MaDonHang] asc, [MaSanPham] asc");
        }

        private int delete(string whereCom)
        {
            string comUpdate = "DELETE FROM [ChiTietDonHang] ";
            if (whereCom.Trim() != "")
                comUpdate += " where " + whereCom;
            try
            {
                IDataProvider dap = new DataProvider(this.connection);
                return dap.ExecuteNonQuery_Command(System.Data.CommandType.Text, comUpdate);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int deleteByKey(string MaDonHang, string MaSanPham)
        {
            return delete(" [MaDonHang]='" + MaDonHang + "' and [MaSanPham]='" + MaSanPham + "' ");
        }

        public int add(ChiTietDonHang obj)
        {
            string comQuery = "INSERT INTO [ChiTietDonHang] ([MaDonHang],[MaSanPham]"
               + ",[SoLuong],[Gia],[GiamGia],[IsStatus])"
               + " VALUES (@MaDonHang,@MaSanPham,@SoLuong,@Gia"
               + ",@GiamGia,@IsStatus)";

            try
            {
                IDataProvider dap = new DataProvider(connection);

                dap.AddParameters("@MaDonHang", obj.MaDonHang, System.Data.SqlDbType.NVarChar);
                dap.AddParameters("@MaSanPham", obj.MaSanPham, System.Data.SqlDbType.NVarChar);
                dap.AddParameters("@SoLuong", obj.SoLuong, System.Data.SqlDbType.Float);
                dap.AddParameters("@Gia", obj.Gia, System.Data.SqlDbType.Decimal);
                dap.AddParameters("@GiamGia", obj.GiamGia, System.Data.SqlDbType.Decimal);
                dap.AddParameters("@IsStatus", obj.IsStatus, System.Data.SqlDbType.Int);

                return dap.ExecuteNonQuery_Command(System.Data.CommandType.Text, comQuery);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        private int update(ChiTietDonHang obj, string whereCom)
        {

            string comQuery = "UPDATE [ChiTietDonHang] SET [MaDonHang]=@MaDonHang,[MaSanPham]=@MaSanPham"
               + ",[SoLuong]=@SoLuong,[Gia]=@Gia,[GiamGia]=@GiamGia,[IsStatus]=@IsStatus ";
            if (whereCom.Trim() != "")
                comQuery += " where " + whereCom;
            try
            {
                IDataProvider dap = new DataProvider(connection);
                dap.AddParameters("@MaDonHang", obj.MaDonHang, System.Data.SqlDbType.NVarChar);
                dap.AddParameters("@MaSanPham", obj.MaSanPham, System.Data.SqlDbType.NVarChar);
                dap.AddParameters("@SoLuong", obj.SoLuong, System.Data.SqlDbType.Float);
                dap.AddParameters("@Gia", obj.Gia, System.Data.SqlDbType.Decimal);
                dap.AddParameters("@GiamGia", obj.GiamGia, System.Data.SqlDbType.Decimal);
                dap.AddParameters("@IsStatus", obj.IsStatus, System.Data.SqlDbType.Int);

                return dap.ExecuteNonQuery_Command(System.Data.CommandType.Text, comQuery);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int updateByKey(ChiTietDonHang data, string MaDonHang, string MaSanPham)
        {
            return update(data, " [MaDonHang]='" + MaDonHang + "' and [MaSanPham]='" + MaSanPham + "' ");
        }

        public int deleteByKey(string MaDonHang)
        {
            return delete(" [MaDonHang]='" + MaDonHang + "'");
        }

    }
}
